package com.huawei.secure.android.common;

import android.content.Context;
import android.os.Build.VERSION;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.X509TrustManager;
import org.apache.http.conn.ssl.BrowserCompatHostnameVerifier;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.StrictHostnameVerifier;
import org.apache.http.conn.ssl.X509HostnameVerifier;

@Deprecated
public class SecureApacheSSLSocketFactory extends SSLSocketFactory {
    public static final X509HostnameVerifier BROWSER_COMPATIBLE_HOSTNAME_VERIFIER = new BrowserCompatHostnameVerifier();
    public static final X509HostnameVerifier STRICT_HOSTNAME_VERIFIER = new StrictHostnameVerifier();
    private static final String d = "TLS";
    private static final String[] e = new String[]{"TEA", "SHA0", "MD2", "MD4", "RIPEMD", "NULL", "RC4", "DES", "DESX", "DES40", "RC2", "MD5", "ANON", "TLS_EMPTY_RENEGOTIATION_INFO_SCSV", "TLS_RSA"};
    private static volatile SecureApacheSSLSocketFactory f = null;
    private SSLContext g;
    private Context mContext;

    private SecureApacheSSLSocketFactory(KeyStore keyStore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
        super(keyStore);
    }

    private SecureApacheSSLSocketFactory(KeyStore keyStore, Context context) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException, CertificateException, IOException, IllegalAccessException {
        super(keyStore);
        this.mContext = context;
        this.g = SSLContext.getInstance(d);
        SecureX509TrustManager secureX509TrustManager = new SecureX509TrustManager(this.mContext);
        this.g.init(null, new X509TrustManager[]{secureX509TrustManager}, new SecureRandom());
    }

    @Deprecated
    public SecureApacheSSLSocketFactory(KeyStore keyStore, InputStream inputStream, String str) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException, CertificateException, IOException {
        super(keyStore);
        this.g = SSLContext.getInstance(d);
        HiCloudX509TrustManager hiCloudX509TrustManager = new HiCloudX509TrustManager(inputStream, str);
        this.g.init(null, new X509TrustManager[]{hiCloudX509TrustManager}, new SecureRandom());
    }

    @Deprecated
    public static SecureApacheSSLSocketFactory getInstance(KeyStore keyStore, Context context) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException, CertificateException, IOException, IllegalAccessException {
        if (f == null) {
            synchronized (SecureApacheSSLSocketFactory.class) {
                if (f == null) {
                    f = new SecureApacheSSLSocketFactory(keyStore, context);
                }
            }
        }
        return f;
    }

    private static void setEnableSafeCipherSuites(SSLSocket sSLSocket) {
        if (sSLSocket != null) {
            String[] enabledCipherSuites = sSLSocket.getEnabledCipherSuites();
            if (enabledCipherSuites != null && enabledCipherSuites.length != 0) {
                List arrayList = new ArrayList();
                String str = "";
                for (String str2 : enabledCipherSuites) {
                    Object obj;
                    String toUpperCase = str2.toUpperCase(Locale.ENGLISH);
                    for (String toUpperCase2 : e) {
                        if (toUpperCase.contains(toUpperCase2.toUpperCase(Locale.ENGLISH))) {
                            obj = 1;
                            break;
                        }
                    }
                    obj = null;
                    if (obj == null) {
                        arrayList.add(str2);
                    }
                }
                sSLSocket.setEnabledCipherSuites((String[]) arrayList.toArray(new String[arrayList.size()]));
            }
        }
    }

    public Socket createSocket(Socket socket, String str, int i, boolean z) throws IOException {
        Socket createSocket = this.g.getSocketFactory().createSocket(socket, str, i, z);
        a(createSocket);
        return createSocket;
    }

    public Socket createSocket() throws IOException {
        Socket createSocket = this.g.getSocketFactory().createSocket();
        a(createSocket);
        return createSocket;
    }

    private void a(Socket socket) {
        if (socket != null && (socket instanceof SSLSocket)) {
            setEnabledProtocols((SSLSocket) socket);
            setEnableSafeCipherSuites((SSLSocket) socket);
        }
    }

    private void setEnabledProtocols(SSLSocket sSLSocket) {
        if (sSLSocket != null && VERSION.SDK_INT >= 16) {
            sSLSocket.setEnabledProtocols(new String[]{"TLSv1.1", "TLSv1.2"});
        }
    }
}
